package com.example.springbootmybatisplusintegrat.user.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springbootmybatisplusintegrat.user.bean.ImportMsg;
import com.example.springbootmybatisplusintegrat.user.bean.User;
import com.example.springbootmybatisplusintegrat.user.service.IUserService;
import com.example.springbootmybatisplusintegrat.user.service.ImportMsgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @Author qrn
 * @Title
 * @Date 2021/4/27 16:44
 * @time 16:44
 */
@Slf4j
@RestController
public class UseController {

    @Autowired
    IUserService iUserService;

    @Autowired
    ImportMsgService importMsgService;

    @GetMapping("getUser")
    public User getUser(Integer id){
        return iUserService.getById(id);
    }

    @GetMapping("list")
    public void list(){
        demo();
//        return iUserService.list();
    }



    public void demo() {
        String fileName = "/Users/qurenneng/xiangmu/mayun/spring-boot-integrate/spring-boot-easyexcel-integrats/src/main/resources/DEVREQ-1896.csv";
        EasyExcel.read(fileName, ImportMsg.class, new ReadListener<ImportMsg>() {
            /**
             * 单次缓存的数据量
             */
            public static final int BATCH_COUNT = 30000;
            /**
             * 临时存储
             */
            private List<ImportMsg> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

            @Override
            public void invoke(ImportMsg data, AnalysisContext context) {
                cachedDataList.add(data);
                if (cachedDataList.size() >= BATCH_COUNT) {
                    saveData();
                    // 存储完成清理 list
                    cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
                }
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {
                saveData();
            }

            /**
             * 加上存储数据库
             */
            private void saveData() {
                log.info("{}条数据，开始存储数据库！", cachedDataList.size());
                importMsgService.saveBatch(cachedDataList);
                log.info("存储数据库成功!");
            }
        }).sheet().doRead();
    }


    /**
     * 分页列表查询:
     * @param page
     * @param rows
     * @return
     */
    @GetMapping("getPageUser")
    public IPage<User> getPageUser(Integer page,Integer rows){
        Page<User> userPage = new Page<User>();
        userPage.setCurrent(page);
        userPage.setSize(rows);
        IPage<User> userIPage = iUserService.selectUserPage(userPage);
        return userIPage;
    }


    @PutMapping("saveUser")
    public boolean saveUser(User user){
        boolean saveuser = iUserService.save(user);
        return  saveuser;
    }

    @DeleteMapping("deleteUser")
    public boolean deleteUser(Integer id){
        boolean removeUserById = iUserService.removeById(id);
            return  removeUserById;
    }

}
